package org.lonetree.tc.webapp.actions;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.Book;
import org.lonetree.tc.core.BookComment;
import org.lonetree.tc.core.Comment;
import org.lonetree.tc.core.Book.Isbn;
import org.lonetree.tc.core.dao.BookDAO;
import org.lonetree.tc.core.dao.DAOFactory;
import org.lonetree.tc.webapp.forms.BookCommentForm;

public class EditBookComment extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
		try{
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.FactoryImplementation.DB_DAO_FACTORY);
			BookDAO bdao = factory.getBookDAO();
			
			if (!request.getMethod().toLowerCase().equals("post"))
			{
				Book book=bdao.getBook(new Isbn(request.getParameter("isbn")));
				request.setAttribute("loaded", book);
				return mapping.findForward("success_get");
			}
			else
			{
				BookCommentForm fm=(BookCommentForm)form;
				Book book=fm.getBook();
				//Set<Comment> editedCommentr
				bdao.updateBook(book);
				request.setAttribute("success", "410");
				return mapping.findForward("success_post");
			}
		}
		
		catch (Exception e)
		{
			return mapping.findForward("failure");
		}
		

	}

}
